Skip to content

bq27441: Fix checksum overflow in compute_block_checksum().#161

Merged
nedseb merged 1 commit intomainfrom
fix/bq27441-checksum-overflow
Mar 16, 2026
Merged

bq27441: Fix checksum overflow in compute_block_checksum().#161
nedseb merged 1 commit intomainfrom
fix/bq27441-checksum-overflow

Conversation

@nedseb
Copy link
Copy Markdown
Contributor

@nedseb nedseb commented Mar 16, 2026

Closes #162

Summary

Fix a pre-existing bug where compute_block_checksum() could produce a negative value when the sum of the 32 data bytes exceeds 255. This caused ValueError: bytes value out of range in _write_reg() during __init__power_on()set_capacity()write_extended_data().

Root cause

csum = 255 - csum  # csum can be > 255, result is negative

Fix

csum = (255 - (csum & 0xFF)) & 0xFF  # mask to byte range

This bug has existed since the initial driver commit but only manifests on real hardware (mock I2C returns zeroed data that stays within range).

Test plan

Mock tests

python3 -m pytest tests/ -k "bq27441 and mock" -v  # 8 passed

Hardware tests

python3 -m pytest tests/ --port /dev/ttyACM0 -k "bq27441 and hardware" -s -v  # 8 passed

@nedseb nedseb requested a review from Copilot March 16, 2026 08:59
@nedseb nedseb self-assigned this Mar 16, 2026
@nedseb nedseb added enhancement New feature or request bug Something isn't working and removed enhancement New feature or request labels Mar 16, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a bug in compute_block_checksum() where the sum of 32 data bytes could exceed 255, causing a negative checksum value and a ValueError when writing to the register. The fix masks the sum to a single byte before computing the complement.

Changes:

  • Mask the intermediate checksum sum to 8 bits before subtracting from 255, preventing negative/out-of-range values.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@nedseb nedseb merged commit cc0dee0 into main Mar 16, 2026
8 checks passed
@nedseb nedseb deleted the fix/bq27441-checksum-overflow branch March 16, 2026 09:03
@semantic-release-updater
Copy link
Copy Markdown

🎉 This PR is included in version 0.0.2 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working released

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bq27441: Fix checksum overflow in compute_block_checksum().

2 participants